using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_LevelHeroHead : UIBaseNode
    {
        #region Template Generate,don't modify

        protected class UIB_UIN_LevelHeroHead
        {
            #region ObjectBinding Generate

            public ExImage hp { protected set; get; }
            public ExImage sp { protected set; get; }
            public ExImage headIcon { protected set; get; }
            public ExButton heroBtn { protected set; get; }
            public CanvasGroup canvasGroup { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<ExImage>("hp", out var __tbv0);
                hp = __tbv0;
                __binding.TryGetVariableValue<ExImage>("sp", out var __tbv1);
                sp = __tbv1;
                __binding.TryGetVariableValue<ExImage>("headIcon", out var __tbv2);
                headIcon = __tbv2;
                __binding.TryGetVariableValue<ExButton>("heroBtn", out var __tbv3);
                heroBtn = __tbv3;
                __binding.TryGetVariableValue<CanvasGroup>("canvasGroup", out var __tbv4);
                canvasGroup = __tbv4;
            }


            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify

        protected UIB_UIN_LevelHeroHead ui { get; set; }

        #region Field

        private ExImage _hp;
        private ExImage _sp;
        private ExImage _heroHead;
        private CanvasGroup _canvasGroup;

        #endregion

        #region Override

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_LevelHeroHead();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            _hp = ui.hp;
            _sp = ui.sp;
            _heroHead = ui.headIcon;
            _canvasGroup = ui.canvasGroup;
            ui.heroBtn.onClick.AddListener(OnHeroBtnClick);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        private void OnHeroBtnClick()
        {
            OpenUI<UIFormation>();
        }

        protected override void OnDispose()
        {
            CodeBasedBlinkEffectGenerator.Instance.Destroy(_canvasGroup);
        }

        #endregion

        public void Refresh(DynLevelHero levelHeroData)
        {
            SetImageSprite(_heroHead, levelHeroData.cfgHeroSkin.BattleHeadIcon_fullPath);
            var nMaxHp = levelHeroData.maxHp;
            var nMaxSp = levelHeroData.maxSp;
            _hp.fillAmount = nMaxHp > 0 ? (float)levelHeroData.hp / nMaxHp : 0;
            _sp.fillAmount = nMaxSp > 0 ? (float)levelHeroData.sp / nMaxSp : 0;
            if (_hp.fillAmount < DyingThreshold)
            {
                CodeBasedBlinkEffectGenerator.Instance.Generate(_canvasGroup, 1f, 1f, 0.2f);
            }
            else
            {
                CodeBasedBlinkEffectGenerator.Instance.Destroy(_canvasGroup);
            }
        }

        private const float DyingThreshold = 0.1f;
    }
}